package shuiyuqing.fragments;

import android.app.Activity;
import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import com.xkh.xkh.R;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;

import shuiyuqing.activities.WaterActivity;
import shuiyuqing.model.BasicInfoModel;
import shuiyuqing.model.Type;
import shuiyuqing.network.beans.IrrBRMeteorological;
import shuiyuqing.network.beans.IrrBRStadia;
import shuiyuqing.views.adapters.WatchPointAdapter;
import utils.TimeUtil;

/**
 * Created by 凯 on 2015/11/7.
 */
public class WeatherFragment extends Fragment {

    // 选择气象站
    View v_selWeatherPoint;

    TextView tv_weatherName;

    TextView tv_rainTime;
    TextView tv_rainValue;
//    TextView tv_weatherTime;
    TextView tv_temperature;
    TextView tv_humidity;
    TextView tv_winddir;
    TextView tv_windspeed;

    TextView tv_tempTime;

    PullToRefreshScrollView pullToRefreshScrollView;

    public TextView getTv_rainTime() {
        return tv_rainTime;
    }

    public View getV_selWeatherPoint() {
        return v_selWeatherPoint;
    }

    public TextView getTv_rainValue() {
        return tv_rainValue;
    }

  /*  public TextView getTv_weatherTime() {
        return tv_weatherTime;
    }*/

    public TextView getTv_temperature() {
        return tv_temperature;
    }

    public TextView getTv_humidity() {
        return tv_humidity;
    }

    public TextView getTv_winddir() {
        return tv_winddir;
    }

    public TextView getTv_windspeed() {
        return tv_windspeed;
    }

    public TextView getTv_weatherName() {
        return tv_weatherName;
    }

    public void setTv_weatherName(TextView tv_weatherName) {
        this.tv_weatherName = tv_weatherName;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_weather, null);

        initview(view);
        initviewwithdata();
        return view;
    }

    // 初始化界面数据
    private void initviewwithdata() {
        IrrBRStadia irrBRStadia = BasicInfoModel.getIrrBRStadiaByCode("10520027");
        tv_weatherName.setText(irrBRStadia.getStadiaName());
        WaterActivity waterActivity = (WaterActivity) getActivity();
        waterActivity.setRainStadia(irrBRStadia);
    }


    View.OnClickListener itemClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == v_selWeatherPoint) {
                WatchPointFragment fragment = new WatchPointFragment();
                WatchPointAdapter adapter = new WatchPointAdapter();
                adapter.setType(Type.RAIN);
                fragment.setWatchPointAdapter(adapter);
                fragment.show(getFragmentManager(), WatchPointFragment.class.getSimpleName());
            }
        }
    };


    // 初始化view
    private void initview(View view) {

        tv_tempTime = (TextView) view.findViewById(R.id.id_tv_tempTime);
        tv_rainTime = (TextView) view.findViewById(R.id.id_tv_rainTime);
        tv_humidity = (TextView) view.findViewById(R.id.id_tv_humidity);
        tv_rainValue = (TextView) view.findViewById(R.id.id_tv_rainValue);
        tv_temperature = (TextView) view.findViewById(R.id.id_tv_temperature);
        tv_winddir = (TextView) view.findViewById(R.id.id_tv_winddir);
        tv_windspeed = (TextView) view.findViewById(R.id.id_tv_windspeed);
//        tv_weatherTime = (TextView) view.findViewById(R.id.id_tv_weatherTime);

        v_selWeatherPoint = view.findViewById(R.id.id_v_weatherPoint);
        v_selWeatherPoint.setOnClickListener(itemClick);

        tv_weatherName = (TextView) view.findViewById(R.id.id_tv_weatherName);
        pullToRefreshScrollView = (PullToRefreshScrollView) view.findViewById(R.id.id_refresh);
        pullToRefreshScrollView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        pullToRefreshScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
                WaterActivity waterActivity = (WaterActivity) getActivity();
                waterActivity.onRefresh();
                pullToRefreshScrollView.onRefreshComplete();
            }
        });

    }

    // 清空界面
    public void clear() {
        tv_rainTime.setText("");
        tv_humidity.setText("");
        tv_rainValue.setText("");
        tv_temperature.setText("");
        tv_winddir.setText("");
        tv_windspeed.setText("");
//        tv_weatherTime.setText("");

    }

    public void setInfo(IrrBRMeteorological irrBRMeteorological) {
        tv_tempTime.setText(TimeUtil.getTimeByDate2(irrBRMeteorological.getMeaTime().getTime()));
        if (TimeUtil.isToday(irrBRMeteorological.getMeaTime()))
        {
            tv_tempTime.setTextColor(Color.parseColor("#FFA500"));
        }
        else
        {
            tv_tempTime.setTextColor(Color.parseColor("#D3D3D3"));
        }

    }


    //
    public static interface Delegate {
        public void setweatherStadia(IrrBRStadia irrBRStadia);
    }
}
